﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krilib.Plugin;

namespace Krile.Forms.PManForm
{
    public partial class PluginManager : Form
    {
        public PluginManager()
        {
            InitializeComponent();
        }

        Cores.PluginData[] pdArray;
        private void PluginManager_Load(object sender, EventArgs e)
        {
            pdArray = Core.PluginManager.GetAllPluginDatas();
            foreach (var pd in pdArray)
            {
                pluginList.Items.Add(pd.Name);
            }
            if (pdArray.Length == 0)
            {
                MessageBox.Show(Lang.Msg.PMan_PluginNotFound, "Plug-in manager", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Close();
            }
            else
            {
                pluginList.SelectedIndex = 0;
            }
        }

        private void pluginList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pluginList.SelectedIndex < 0) return;
            var cpd = pdArray[pluginList.SelectedIndex];
            nameText.Text = cpd.Name;
            pathText.Text = cpd.FilePath;
            classText.Text = cpd.ClassName;
            descText.Text = cpd.Description;
            loadPluginCheck.Checked = cpd.Loadable;
        }

        private void reloadDesc_Click(object sender, EventArgs e)
        {
            if (pluginList.SelectedIndex < 0) return;
            reloadDesc.Enabled = false;
            pdArray[pluginList.SelectedIndex].ReloadDescription();
            reloadDesc.Enabled = true;
        }

        private void loadPluginCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (pluginList.SelectedIndex < 0) return;
            if (pdArray[pluginList.SelectedIndex].Loadable == loadPluginCheck.Checked)
                return;
            if (Core.PluginManager.PluginAuth.SetAuthorize(pdArray[pluginList.SelectedIndex], loadPluginCheck.Checked))
                MessageBox.Show(Lang.Msg.PMan_PluginStateChanged, "Plug-in manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                loadPluginCheck.Checked = !loadPluginCheck.Checked;
        }
    }
}
